Unify out of memory libc handling.
authorrobertl <robertl@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Wed, 18 Sep 2002 16:37:48 +0000 (16:37 +0000)
committerrobertl <robertl@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Wed, 18 Sep 2002 16:37:48 +0000 (16:37 +0000)
Update dependencies.

23 files changed:
gpsbabel/Makefile
gpsbabel/cetus.c
gpsbabel/csv.c
gpsbabel/csv_util.c
gpsbabel/defs.h
gpsbabel/garmin.c
gpsbabel/geo.c
gpsbabel/gpsman.c
gpsbabel/gpspilot.c
gpsbabel/gpsutil.c
gpsbabel/gpx.c
gpsbabel/holux.c
gpsbabel/magnav.c
gpsbabel/magproto.c
gpsbabel/mapsend.c
gpsbabel/mapsource.c
gpsbabel/mkshort.c
gpsbabel/mxf.c
gpsbabel/ozi.c
gpsbabel/pcx.c
gpsbabel/psp.c
gpsbabel/tiger.c
gpsbabel/util.c

index a81e2e9d6b40816bbd04f465fc1457824873ee76..4fb6dfbf2b014ec57393b394a4f7159512525135 100644 (file)
@@ -17,32 +17,38 @@ OBJS=main.o queue.o route.o waypt.o util.o vecs.o mkshort.o csv_util.o \
 all: gpsbabel
 
 gpsbabel: $(OBJS)
-       $(CC) $(CFLAGS) $(OBJS) -o gpsbabel -lexpat -lm #-lpdb
+       $(CC) $(CFLAGS) $(OBJS) -o gpsbabel -lexpat -lm
 
 clean:
        rm -f $(OBJS) gpsbabel gpsbabel.exe
 
 cetus.o: cetus.c defs.h queue.h coldsync/palm.h coldsync/pdb.h
-csv.o: csv.c defs.h queue.h
+csv.o: csv.c defs.h queue.h csv_util.h
+csv_util.o: csv_util.c defs.h queue.h csv_util.h
+garmin.o: garmin.c defs.h queue.h jeeps/gps.h jeeps/gpsport.h \
+  jeeps/gpsserial.h jeeps/gpssend.h jeeps/gpsread.h jeeps/gpsutil.h \
+  jeeps/gpsapp.h jeeps/gpsprot.h jeeps/gpscom.h jeeps/gpsfmt.h \
+  jeeps/gpsmath.h jeeps/gpsnmea.h jeeps/gpsmem.h jeeps/gpsrqst.h \
+  jeeps/gpsinput.h jeeps/gpsproj.h jeeps/gpsnmeafmt.h jeeps/gpsnmeaget.h
 geo.o: geo.c defs.h queue.h
 gpsman.o: gpsman.c defs.h queue.h
 gpspilot.o: gpspilot.c defs.h queue.h coldsync/palm.h coldsync/pdb.h
 gpsutil.o: gpsutil.c defs.h queue.h magellan.h
 gpx.o: gpx.c defs.h queue.h
+holux.o: holux.c defs.h queue.h holux.h
 magnav.o: magnav.c defs.h queue.h coldsync/palm.h coldsync/pdb.h
 magproto.o: magproto.c defs.h queue.h magellan.h
 main.o: main.c defs.h queue.h
 mapsend.o: mapsend.c defs.h queue.h mapsend.h
 mapsource.o: mapsource.c defs.h queue.h
-mkshort.o: mkshort.c
+mkshort.o: mkshort.c defs.h queue.h
+mxf.o: mxf.c defs.h queue.h csv_util.h
+ozi.o: ozi.c defs.h queue.h csv_util.h
 pcx.o: pcx.c defs.h queue.h
+psp.o: psp.c defs.h queue.h
 queue.o: queue.c queue.h
 route.o: route.c defs.h queue.h
 tiger.o: tiger.c defs.h queue.h magellan.h
 util.o: util.c defs.h queue.h
 vecs.o: vecs.c defs.h queue.h
 waypt.o: waypt.c defs.h queue.h
-psp.o: psp.c defs.h queue.h
-mxf.o: mxf.c csv_util.c defs.h queue.h csv_util.h
-ozi.o: ozi.c csv_util.c defs.h queue.h csv_util.h
-csv_util.o: csv_util.c csv_util.h defs.h 
index 8443adaa4795fc8bfb873b1a8e31e26e0da0c614..c834ab67bc7ce6999628f22c2a47c88abab1d745 100644 (file)
@@ -108,14 +108,11 @@ data_read(void)
        for(pdb_rec = pdb->rec_index.rec; pdb_rec; pdb_rec=pdb_rec->next) {
                waypoint *wpt_tmp;
 
-               wpt_tmp = calloc(sizeof(*wpt_tmp),1);
-               if (wpt_tmp == NULL) {
-                       fatal(MYNAME ": cannot allocate memory\n");
-               }
+               wpt_tmp = xcalloc(sizeof(*wpt_tmp),1);
 
                rec = (struct record *) pdb_rec->data;
-               wpt_tmp->shortname = strdup(rec->ID);
-               wpt_tmp->description = strdup(rec->name);
+               wpt_tmp->shortname = xstrdup(rec->ID);
+               wpt_tmp->description = xstrdup(rec->name);
                wpt_tmp->position.altitude.altitude_meters = pdb_read4(&rec->elevation) / 100.0;
 
                wpt_tmp->position.longitude.degrees = pdb_read4(&rec->longitude) / 10000000.0; 
@@ -146,7 +143,7 @@ cetus_writewpt(waypoint *wpt)
        static int ct;
        struct tm *tm;
 
-       rec = calloc(sizeof(*rec),1);
+       rec = xcalloc(sizeof(*rec),1);
 
        strncpy(rec->ID, wpt->shortname, sizeof(rec->ID));
        rec->ID[sizeof(rec->ID)-1] = 0;
@@ -226,11 +223,9 @@ data_write(void)
         * Turns out plain old strcmp will do the trick...
         */
 
-       htable = malloc(ct * sizeof(*htable));
+       htable = xmalloc(ct * sizeof(*htable));
        bh = htable;
-       if (!htable) {
-               fatal (MYNAME ":Cannot get array for sorting waypoints.");
-       }
+
         QUEUE_FOR_EACH(&waypt_head, elem, tmp) {
                 waypointp = (waypoint *) elem;
                bh->wpt = waypointp;
index a7ba546a88a124df1a161737c01591e2f58cfcd7..8358b698fe493ca67b29b73c2038d6d1001a4e3f 100644 (file)
@@ -75,10 +75,7 @@ data_read(void)
 
                if (strlen(buff)) {
 
-                   wpt_tmp = calloc(sizeof(*wpt_tmp), 1);
-                   if (wpt_tmp == NULL) {
-                       fatal(MYNAME ": cannot allocate memory\n");
-                   }
+                   wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1);
 
                    s = buff;
                    /* data delimited by commas, not enclosed */
@@ -95,9 +92,7 @@ data_read(void)
                                wpt_tmp->position.longitude.degrees = atof(s);
                                break;
                        case 2:
-                               wpt_tmp->description = strdup(s);
-                               if (! wpt_tmp->description) 
-                                   fatal(MYNAME, ": cannot allocate memory\n");
+                               wpt_tmp->description = xstrdup(s);
                                wpt_tmp->description = csv_stringtrim(wpt_tmp->description, " ");
                                break;
                        default:
index 4471b7bd112308776e6e83b2ae5350cebc07b76a..35e18bd5e7ac3d4598e370a007f55f02e2b74694 100644 (file)
@@ -180,11 +180,7 @@ csv_lineparse(char *stringstart, const char *delimited_by,
     }
 
     /* allocate enough space for this data field */
-    tmp = (char *) calloc((p - sp) + 1, sizeof(char));
-
-    if (!tmp) {
-       fatal(MYNAME ": cannot allocate memory\n");
-    }
+    tmp = xcalloc((p - sp) + 1, sizeof(char));
 
     strncpy(tmp, sp, (p - sp));
 
index 9ca580c8537d9688c6e31ddac00701abca6e3bcb..f8d892fdba2205028bd0425f92652a0df666990a 100644 (file)
@@ -112,10 +112,12 @@ void waypt_disp(waypoint *);
 void fatal(const char *, ...);
 ff_vecs_t *find_vec(char *);
 void disp_vecs(void);
-
-
 void printposn(coord *c, int is_lat);
 
+void *xcalloc(size_t nmemb, size_t size);
+void *xmalloc(size_t size);
+char * xstrdup(const char *s);
+
 /* 
  * Data types for Palm/OS files.
  */
index 5b586b4f9f1a4f91b6034e75086e93098bfe930a..7d25309885c121555be200926e71df3d7600b29e 100644 (file)
@@ -54,7 +54,7 @@ data_read(void)
        }
 
        for (i = 0; i < n; i++) {
-               waypoint *wpt_tmp = calloc(sizeof(*wpt_tmp),1);
+               waypoint *wpt_tmp = xcalloc(sizeof(*wpt_tmp),1);
 
                wpt_tmp->shortname = way[i]->ident;
                wpt_tmp->description = way[i]->cmnt;
@@ -77,9 +77,8 @@ data_write(void)
        extern queue waypt_head;
        GPS_PWay *way;
 
+       way = xmalloc(n*sizeof(*way));
 
-       if(!(way=(GPS_PWay *)malloc(n*sizeof(GPS_PWay *)))) 
-               fatal(MYNAME ":not enough memory\n");
        for (i = 0; i < n; i++) {
                if(!((way)[i]=GPS_Way_New()))
                        fatal(MYNAME ":not enough memory\n");
index 09a0c8d7ef540bb97f7ff6f695593ec217de44e4..1333c20f855bd537a22f15d875f36c4e50f64ce4 100644 (file)
@@ -56,7 +56,7 @@ tag_name(const char **attrv)
        const char **avp = &attrv[0];
        while (*avp) { 
                if (strcmp(avp[0], "id") == 0) {
-                       wpt_tmp->shortname = strdup(avp[1]);
+                       wpt_tmp->shortname = xstrdup(avp[1]);
                }
                avp+=2;
        }
@@ -68,7 +68,7 @@ tag_link(const char **attrv)
        const char **avp = &attrv[0];
        while (*avp) { 
                if (strcmp(avp[0], "text") == 0) {
-                       wpt_tmp->url_link_text = strdup(avp[1]);
+                       wpt_tmp->url_link_text = xstrdup(avp[1]);
                }
                avp+=2;
        }
@@ -91,7 +91,7 @@ geo_start(void *data, const char *el, const char **attr)
        }
 
        if (strcmp(el, "waypoint") == 0) {
-               wpt_tmp = calloc(sizeof(*wpt_tmp), 1);
+               wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1);
                in_wpt++;
        } else if (strcmp(el, "name") == 0) {
                in_name++;
@@ -119,7 +119,7 @@ geo_end(void *data, const char *el)
 static void
 geo_cdata(void *dta, const XML_Char *s, int len)
 {
-       char *foo = malloc(len+1);
+       char *foo = xmalloc(len+1);
        foo[len] = 0;
        strncpy(foo, s, len);
        if (in_name) {
index c67f0af6d1afb777deaea87fe2be9a8989965b32..797fe4aa4ffaa2814d19a1901b5233add5790225 100644 (file)
@@ -96,11 +96,7 @@ gpsman_read(void)
                sscanf(ibuf, "%[^\t] %[^\t] %c%d %lf %c%d %lf",
                        sname, descr, &latdir, &latm, &latf, &londir, &lonm, &lonf);
 
-               wpt_tmp = calloc(sizeof(*wpt_tmp),1);
-
-               if (wpt_tmp == NULL) {
-                       fatal(MYNAME ": Cannot allocate enough memory\n");
-               }
+               wpt_tmp = xcalloc(sizeof(*wpt_tmp),1);
 
                lat = latm + latf;
                lon = lonm + lonf;
@@ -110,8 +106,8 @@ gpsman_read(void)
 
                wpt_tmp->position.longitude.degrees = lon;
                wpt_tmp->position.latitude.degrees = lat;
-               wpt_tmp->shortname = strdup(sname);
-               wpt_tmp->description = strdup(descr);
+               wpt_tmp->shortname = xstrdup(sname);
+               wpt_tmp->description = xstrdup(descr);
 
                waypt_add(wpt_tmp);
        }
index ed124d6f86c60fe75db89930c2587eb74730a1c7..22e15416545a1e9464f70501d3c8d49ce8d0f1bf 100644 (file)
@@ -90,10 +90,7 @@ data_read(void)
                waypoint *wpt_tmp;
                char *vdata;
 
-               wpt_tmp = calloc(sizeof(*wpt_tmp),1);
-               if (wpt_tmp == NULL) {
-                       fatal(MYNAME ": cannot allocate memory\n");
-               }
+               wpt_tmp = xcalloc(sizeof(*wpt_tmp),1);
 
                rec = (struct record *) pdb_rec->data;
                wpt_tmp->position.longitude.degrees = pdb_read4(&rec->longitude) / 3.6e6; 
@@ -110,11 +107,11 @@ data_read(void)
                 * receiver) we use that for shortname and use 'name' as
                 * our description.
                 */
-               wpt_tmp->description = strdup(vdata);
+               wpt_tmp->description = xstrdup(vdata);
                vdata = vdata + strlen(vdata) + 1;
 
                vdata = vdata + strlen(vdata) + 1;
-               wpt_tmp->shortname = strdup(vdata);
+               wpt_tmp->shortname = xstrdup(vdata);
                
                waypt_add(wpt_tmp);
 
@@ -130,7 +127,7 @@ cetus_writewpt(waypoint *wpt)
        static int ct;
        struct tm *tm;
 
-       rec = calloc(sizeof(*rec),1);
+       rec = xcalloc(sizeof(*rec),1);
 #if FIXME
        strncpy(rec->ID, wpt->shortname, sizeof(rec->ID));
        rec->ID[sizeof(rec->ID)-1] = 0;
index 550d83113d7fe6a96878054d10b73a0c5235b4cf..6e10c1d9b3c23b9863e0e58e7401ba4bb828f501 100644 (file)
@@ -55,13 +55,10 @@ data_read(void)
        while( fscanf(file_in, "%s %le%c %le%c %ld%c %30[^,] %c",
                        name, &lat, &latdir, &lon, &londir,
                        &alt, &alttype, desc, icon) > 0) {
-               wpt_tmp = calloc(sizeof(*wpt_tmp),1);
-               if (wpt_tmp == NULL) {
-                       fatal(MYNAME ": cannot allocate memory\n");
-               }
+               wpt_tmp = xcalloc(sizeof(*wpt_tmp),1);
                wpt_tmp->position.altitude.altitude_meters = alt;
-               wpt_tmp->shortname = strdup(name);
-               wpt_tmp->description = strdup(desc);
+               wpt_tmp->shortname = xstrdup(name);
+               wpt_tmp->description = xstrdup(desc);
                wpt_tmp->creation_time = time(NULL);
 
                if (latdir == 'S') lat = -lat;
@@ -73,7 +70,7 @@ data_read(void)
                wpt_tmp->position.longitude.degrees = ilon + (lon - ilon)*(100.0/60.0);
                ilat = (int)(lat);
                wpt_tmp->position.latitude.degrees = ilat + (lat - ilat) * (100.0/60.0);
-               wpt_tmp->icon_descr = strdup(icon);
+               wpt_tmp->icon_descr = xstrdup(icon);
 
                waypt_add(wpt_tmp);
        }
index 2c681ccaa81ab4840c67236cf3101615a4ba8610..97c417725b0decf41707053d6a377f27967d73a2 100644 (file)
@@ -60,10 +60,7 @@ tag_wpt(const char **attrv)
 {
        const char **avp = &attrv[0];
 
-       wpt_tmp = calloc(sizeof(*wpt_tmp), 1);
-       if (wpt_tmp == NULL) {
-               fatal(MYNAME ": allocate memory\n");
-       }
+       wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1);
 
        while (*avp) { 
                if (strcmp(avp[0], "lat") == 0) {
@@ -125,7 +122,7 @@ gpx_end(void *data, const char *el)
 static void
 gpx_cdata(void *dta, const XML_Char *s, int len)
 {
-       char *foo = malloc(len+1);
+       char *foo = xmalloc(len+1);
        foo[len] = 0;
        strncpy(foo, s, len);
        if (in_name && in_wpt) {
index 2fbf876e72d101e94f4d5e0ddae2bf05074517a4..0bc0bd1e610a15a77d3d80991707faf59939d86f 100644 (file)
@@ -58,13 +58,9 @@ static void rd_deinit(void)
 static void
 wr_init(const char *fname)
 {
-       HxWFile = calloc(GM100_WPO_FILE_SIZE, 1);
-       if (HxWFile == NULL) 
-    {
-               fatal("GPSBABEL: Cannot alloc memory\n");
-    }
+       HxWFile = xcalloc(GM100_WPO_FILE_SIZE, 1);
 
-    strcpy (fOutname,fname);
+       strcpy (fOutname,fname);
 }
 
 
@@ -95,11 +91,7 @@ static void data_read(void)
        struct tm *ptm;
 
 
-       HxWpt = calloc(GM100_WPO_FILE_SIZE, 1);
-       if (HxWpt == NULL) 
-    {
-               fatal("GPSBABEL: Cannot alloc memory\n");
-    }
+    HxWpt = xcalloc(GM100_WPO_FILE_SIZE, 1);
 
     /* read the wpo file to the data-array */
     iDataRead = fread( HxWpt, 1, GM100_WPO_FILE_SIZE, file_in );
@@ -115,7 +107,7 @@ static void data_read(void)
     /* Get the waypoints */
     for (iCount = 0; iCount < iWptNum ; iCount ++)
     {
-        wpt_tmp = calloc(sizeof(*wpt_tmp), 1);
+        wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1);
     
         iWptIndex = ((WPTHDR *)HxWpt)->idx[iCount];         /* get the waypoint index  */
         dwIndex= OFFS_WPT + (sizeof(WPT) * iWptIndex);
@@ -128,8 +120,8 @@ static void data_read(void)
         strncpy(desc,pWptHxTmp->comment,sizeof(pWptHxTmp->comment));
         desc[sizeof(pWptHxTmp->comment)]=0;
         
-               wpt_tmp->shortname = strdup(name);
-               wpt_tmp->description = strdup(desc);
+               wpt_tmp->shortname = xstrdup(name);
+               wpt_tmp->description = xstrdup(desc);
 
                wpt_tmp->creation_time = 0;
         if (pWptHxTmp->date.year)
index 39da81c9956e39944412e105ac97acf1d9e4f2d0..699b27f76c7ac275facd7ac2fb746fd88eb46b88 100644 (file)
@@ -105,11 +105,7 @@ data_read(void)
                char *vdata;
                struct tm tm = {0};
 
-               wpt_tmp = calloc(sizeof(*wpt_tmp),1);
-               if (wpt_tmp == NULL) {
-                       fatal(MYNAME ": cannot allocate memory\n");
-               }
-
+               wpt_tmp = xcalloc(sizeof(*wpt_tmp),1);
                rec = (struct record *) pdb_rec->data;
                wpt_tmp->position.altitude.altitude_meters = pdb_read4(&rec->elevation); 
 
@@ -118,10 +114,10 @@ data_read(void)
 
                vdata = (char *) pdb_rec->data + sizeof(*rec);
 
-                wpt_tmp->shortname = strdup(vdata);
+                wpt_tmp->shortname = xstrdup(vdata);
                vdata += strlen (vdata) + 1;
 
-               wpt_tmp->description = strdup(vdata);
+               wpt_tmp->description = xstrdup(vdata);
                vdata += strlen (vdata) + 1;
                
                tm.tm_sec = pdb_read2(&rec->crt_sec);
@@ -146,7 +142,7 @@ my_writewpt(waypoint *wpt)
        static int ct;
        struct tm *tm;
 abort();
-       rec = calloc(sizeof(*rec),1);
+       rec = xcalloc(sizeof(*rec),1);
 #if 0
        strncpy(rec->ID, wpt->shortname, sizeof(rec->ID));
        rec->ID[sizeof(rec->ID)-1] = 0;
index fd1444afe5fb38b8f0e2579c14bd28fb58bb00b8..86f7d92116c03e3a012408e7a67598582ccaef0a 100644 (file)
@@ -562,9 +562,7 @@ mag_trkparse(char *trkmsg)
        struct tm tm;
        waypoint *waypt;
 
-       waypt  = calloc(sizeof *waypt, 1);
-       if (waypt == NULL) 
-               return NULL;
+       waypt  = xcalloc(sizeof *waypt, 1);
 
        printf("%s\n", trkmsg);
        memset(&tm, 0, sizeof(tm));
@@ -678,9 +676,7 @@ mag_wptparse(char *trkmsg)
        descr[0] = 0;
        icon_token[0] = 0;
 
-       waypt  = calloc(sizeof *waypt, 1);
-       if (waypt == NULL) 
-               fatal(MYNAME ": Cannot allocate memory\n");
+       waypt  = xcalloc(sizeof *waypt, 1);
 
        sscanf(trkmsg,"$PMGNWPL,%lf,%c,%lf,%c,%d,%c,%[^,],%[^,]", 
                &latdeg,&latdir,
@@ -699,8 +695,8 @@ mag_wptparse(char *trkmsg)
        waypt->position.longitude.degrees = mag2degrees(lngdeg);
 
        waypt->position.altitude.altitude_meters = alt;
-       waypt->shortname = strdup(shortname);
-       waypt->description = strdup(descr);
+       waypt->shortname = xstrdup(shortname);
+       waypt->description = xstrdup(descr);
        waypt->icon_descr = mag_find_descr_from_token(icon_token);
 
        return waypt;
index 0e4dd6e9606e69e93bfa3ae5c9acbe36d4a22568..7d9d3ac41b840012b0d0ecc1974df93790c24b33 100644 (file)
@@ -206,7 +206,7 @@ mapsend_read(void)
        my_fread4(&wpt_count, mapsend_file_in);
 
        while (wpt_count--) {
-               wpt_tmp = calloc(sizeof(*wpt_tmp), 1);
+               wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1);
 
                fread(&scount, sizeof(scount), 1, mapsend_file_in);
                fread(tbuf, scount, 1, mapsend_file_in);
@@ -225,8 +225,8 @@ mapsend_read(void)
                my_fread8(&wpt_long, mapsend_file_in);
                my_fread8(&wpt_lat, mapsend_file_in);
 
-               wpt_tmp->shortname = strdup(name);
-               wpt_tmp->description = strdup(comment);
+               wpt_tmp->shortname = xstrdup(name);
+               wpt_tmp->description = xstrdup(comment);
                wpt_tmp->position.altitude.altitude_meters = wpt_alt;
                wpt_tmp->position.latitude.degrees = -wpt_lat;
                wpt_tmp->position.longitude.degrees = wpt_long;
index da715d28728159ce36d5cbe260da115cefe9f954..fe675a5960d65ed1c214202b21be68ad2e847b6f 100644 (file)
@@ -78,14 +78,11 @@ mapsource_read(void)
                sscanf(ibuf,
                "Waypoint %s %s %s %c%d %f %c%d %f %d %s Symbol & Name %s",
                 name, date, timeb, &latdir, &latd, &latf, &londir, &lond, &lonf, &alt, altunits, icon);
-               wpt_tmp = calloc(sizeof(*wpt_tmp),1);
-               if (wpt_tmp == NULL) {
-                       fatal("MAPSOURCE: cannot allocate memory\n");
-               }
+               wpt_tmp = xcalloc(sizeof(*wpt_tmp),1);
 /* FIXME: Implement actual appropriate conversion */
                wpt_tmp->position.altitude.altitude_meters = alt * 3.0;
-               wpt_tmp->shortname = strdup(name);
-               wpt_tmp->description = strdup(name);
+               wpt_tmp->shortname = xstrdup(name);
+               wpt_tmp->description = xstrdup(name);
 
                lat = latd + latf/100.0;
                lon = lond + lonf/100.0;
index 4ca5c499e90267daca30cb685680b7e8fd62f634..4fde0e11dd85bb9e951dd617440494721b78e80f 100644 (file)
@@ -32,7 +32,7 @@ delete_last_vowel(int start, char *istring, int *replaced)
         */
        for (l = strlen(istring); l > start; l--) {
                if (strchr(vowels, istring[l-1])) {
-                       char *ostring = strdup(istring);
+                       char *ostring = xstrdup(istring);
 
                        strncpy(&ostring[l-1], &istring[l], 1+strlen(istring)-l);
                        ostring[strlen(istring)-1] = 0;
@@ -76,7 +76,7 @@ setshort_badchars(const char *s)
        if (s == NULL) {
                badchars = DEFAULT_BADCHARS;
        } else {
-               badchars = strdup(s);
+               badchars = xstrdup(s);
        }
 }
 
@@ -89,28 +89,20 @@ setshort_mustupper(int i)
 char *
 mkshort(const char *istring)
 {
-       char *ostring = strdup(istring);
+       char *ostring = xstrdup(istring);
        char *nstring;
        char *tstring;
        char *cp;
        char *np;
        int i, l, nlen, replaced;
 
-
-       if (!ostring) {
-               fatal("mkshort: could not reallocate memory for string\n");
-       }
-
        /* 
         * Whack leading "[Tt]he",
         */
        if (( strlen(ostring) > target_len + 4) && 
            (strncmp(ostring, "The ", 4) == 0 || 
            strncmp(ostring, "the ", 4) == 0)) {
-               nstring = strdup(ostring + 4);
-               if (!nstring) {
-                       fatal(needmem);
-               }
+               nstring = xstrdup(ostring + 4);
                free(ostring);
                ostring = nstring;
        }
@@ -119,7 +111,7 @@ mkshort(const char *istring)
         * Look at the back of the string for " by BLAH" and whack 
         * it there.
         */
-       nstring = strdup(ostring);
+       nstring = xstrdup(ostring);
        l = strlen (nstring);
        while (l > 0) {
                if (strncmp(&nstring[l], " by ",4) == 0)  {
@@ -135,10 +127,7 @@ mkshort(const char *istring)
                /* 
                 * Eliminate Whitespace 
                 */
-               tstring = strdup(ostring);
-               if (!tstring) {
-                       abort();
-               }
+               tstring = xstrdup(ostring);
                l = strlen (tstring);
                cp = ostring;
                for (i=0;i<l;i++) {
@@ -156,10 +145,7 @@ mkshort(const char *istring)
        /*
         * Eliminate chars on the blacklist.
         */
-       tstring = strdup(ostring);
-       if (!tstring) {
-               fatal(needmem);
-       }
+       tstring = xstrdup(ostring);
        l = strlen (tstring);
        cp = ostring;
        for (i=0;i<l;i++) {
@@ -181,10 +167,7 @@ mkshort(const char *istring)
         * both.
         */
 
-       tstring = strdup(ostring);
-       if (!tstring) {
-               fatal(needmem);
-       }
+       tstring = xstrdup(ostring);
 
        /*
         * Delete vowels starting from the end.  If it fits, quit stomping
index 47385f7580088754901a7488519e1967f31371f2..2641bcb1e064de3cc20cc5285e7337a8854da11b 100644 (file)
@@ -83,11 +83,7 @@ data_read(void)
 
        if (strlen(buff)) {
 
-           wpt_tmp = calloc(sizeof(*wpt_tmp), 1);
-
-           if (wpt_tmp == NULL) {
-               fatal(MYNAME ": cannot allocate memory\n");
-           }
+           wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1);
 
            /* data delimited by commas, possibly enclosed in quotes.  */
            s = buff;
@@ -103,17 +99,11 @@ data_read(void)
                    wpt_tmp->position.longitude.degrees = atof(s);
                    break;
                case 2:
-                   wpt_tmp->description = strdup(s);
-                   if (! wpt_tmp->description) 
-                       fatal(MYNAME, ": cannot allocate memory\n");
-
+                   wpt_tmp->description = xstrdup(s);
                    wpt_tmp->description = csv_stringtrim(wpt_tmp->description, "");
                    break;
                case 3:
-                   wpt_tmp->shortname = strdup(s);
-                   if (! wpt_tmp->shortname) 
-                       fatal(MYNAME, ": cannot allocate memory\n");
-
+                   wpt_tmp->shortname = xstrdup(s);
                    csv_stringtrim(wpt_tmp->shortname, "");
                    break;
                case 4:
index 79841f6a034811bf2f943c5f36479eead7a02bcf..91f0bd2e45340b732c51e139e9529a087841b6fb 100644 (file)
@@ -81,11 +81,7 @@ data_read(void)
 
        if ((strlen(buff)) && (strstr(buff, ",") != NULL)) {
 
-           wpt_tmp = calloc(sizeof(*wpt_tmp), 1);
-
-           if (wpt_tmp == NULL) {
-               fatal(MYNAME ": cannot allocate memory\n");
-           }
+           wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1);
 
            /* data delimited by commas, possibly enclosed in quotes.  */
            s = buff;
@@ -99,11 +95,7 @@ data_read(void)
                    break;
                case 1:
                    /* waypoint name */
-                   wpt_tmp->shortname = strdup(s);
-
-                   if (! wpt_tmp->shortname) 
-                       fatal(MYNAME, ": cannot allocate memory\n");
-
+                   wpt_tmp->shortname = xstrdup(s);
                    csv_stringtrim(wpt_tmp->shortname, "");
                    break;
                case 2:
@@ -135,11 +127,7 @@ data_read(void)
                    break;
                case 10:
                    /* Description */
-                   wpt_tmp->description = strdup(s);
-
-                   if (! wpt_tmp->description) 
-                       fatal(MYNAME, ": cannot allocate memory\n");
-
+                   wpt_tmp->description = xstrdup(s);
                    wpt_tmp->description = csv_stringtrim(wpt_tmp->description, "");
 
                    break;
index dfd45aa0a71b20d64700aeea6dfd6b9ab10f907a..9b5b347c5e834caf966ee286f2b0fad7e57c1c88 100644 (file)
@@ -78,16 +78,16 @@ data_read(void)
                        sscanf(ibuf, "W  %s %c%lf %c%lf %s %s %ld %90[^\n']", 
                                name, &latdir, &lat, &londir, &lon, 
                                date, time, &alt, desc);
-               wpt_tmp = calloc(sizeof(*wpt_tmp), 1);
+               wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1);
                wpt_tmp->position.altitude.altitude_meters = alt;
-               wpt_tmp->shortname = strdup(name);
-               wpt_tmp->description = strdup(desc);
+               wpt_tmp->shortname = xstrdup(name);
+               wpt_tmp->description = xstrdup(desc);
 
                if (latdir == 'S') lat = -lat;
                if (londir == 'W') lon = -lon;
                wpt_tmp->position.longitude.degrees = lon/100.0;
                wpt_tmp->position.latitude.degrees = lat/100.0;
-               wpt_tmp->icon_descr = strdup(icon);
+               wpt_tmp->icon_descr = xstrdup(icon);
                waypt_add(wpt_tmp);
                }
        }
index 98baa83ce03c23eee7f616129f9d0fcb1527dd49..826ec6418778728ddffd53de2a33d48a0114a3bf 100644 (file)
@@ -135,11 +135,7 @@ psp_read(void)
        pincount = *(short int *)&buff[12];
 
        while (pincount--) {
-           wpt_tmp = calloc(sizeof(*wpt_tmp),1);
-
-           if (wpt_tmp == NULL) {
-               fatal(MYNAME ": cannot allocate memory\n");
-           }
+           wpt_tmp = xcalloc(sizeof(*wpt_tmp),1);
 
            /* things we will probably never know about this waypoint */
            /* coming from a pushpin file.                            */
@@ -198,7 +194,7 @@ psp_read(void)
 
            buffer_washer(buff, stringsize);
 
-           wpt_tmp->shortname = strdup(buff);
+           wpt_tmp->shortname = xstrdup(buff);
 
             /* 1 bytes string size */
            psp_fread(&buff[0], 1, 1, psp_file_in);
@@ -215,7 +211,7 @@ psp_read(void)
 
            buffer_washer(buff, stringsize);
 
-           wpt_tmp->description = strdup(buff);
+           wpt_tmp->description = xstrdup(buff);
 
             /* 1 bytes - string size */
            psp_fread(&buff[0], 1, 1, psp_file_in);
index 5978153ebe18bfa3fad91bf0a2d47b957faa0001..34a5196c1d3a69686ffcfe7d23787fbe933d903f 100644 (file)
@@ -73,20 +73,17 @@ abort();
        while( fscanf(file_in, "%s %le%c %le%c %ld%c %30[^,] %c",
                        name, &lat, &latdir, &lon, &londir,
                        &alt, &alttype, desc, icon) > 0) {
-               wpt_tmp = calloc(sizeof(*wpt_tmp),1);
-               if (wpt_tmp == NULL) {
-                       fatal(MYNAME ": cannot allocate memory\n");
-               }
+               wpt_tmp = xcalloc(sizeof(*wpt_tmp),1);
                wpt_tmp->position.altitude.altitude_meters = alt;
-               wpt_tmp->shortname = strdup(name);
-               wpt_tmp->description = strdup(desc);
+               wpt_tmp->shortname = xstrdup(name);
+               wpt_tmp->description = xstrdup(desc);
                wpt_tmp->creation_time = time(NULL);
 
                if (latdir == 'S') lat = -lat;
                if (londir == 'W') lon = -lon;
                wpt_tmp->position.longitude.degrees = lon/100.0;
                wpt_tmp->position.latitude.degrees = lat/100.0;
-               wpt_tmp->icon_descr = strdup(icon);
+               wpt_tmp->icon_descr = xstrdup(icon);
 
                waypt_add(wpt_tmp);
        }
index e70bf7f54e16f05a873fa18815d636e860e6d4ba..68504ae22b79b21140197053749562507f82c464 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 
+void *
+xmalloc(size_t size)
+{
+       void *obj = malloc(size);
+
+       if (!obj) {
+               fatal("gpsbabel: Unable to allocate %d bytes of memory.\n");
+       }
+
+       return obj;
+}
+
+void *
+xcalloc(size_t nmemb, size_t size)
+{
+       void *obj = calloc(nmemb, size);
+
+       if (!obj) {
+               fatal("gpsbabel: Unable to allocate %d bytes of memory.\n");
+       }
+
+       return obj;
+}
+
+char *
+xstrdup(const char *s)
+{
+       char *o = strdup(s);
+
+       if (!o) {
+               fatal("gpsbabel: Unable to allocate %d bytes of memory.\n");
+       }
+
+       return o;
+}
+
+
 coord
 mkposn(const char *string)
 {